using System;
using Mono.Unix;
using Gtk;
using bashpromptutil;
using System.Collections;
using System.Collections.Generic;

namespace bashpromptutil
{
	
	
	public class bashPreview
	{
		//code to generate gdi+ preview
		
		//fields
		private string _wizard; //string from wizardBox
		//private string[] _wPrompt; //input the wizard prompt for processing
		//private string[] _bPrompt; //input the bash escape sequence prompt for processing
		//private bool _type; //the format of prompt we're getting: 0=wizard,1=manual
		private string[] _colors; //queue of colors
		private string[] _params; //queue of parameters (items to be colored)
		
		//properties
		public string[] colors
		{
			get { return _colors; }
		}
		public string[] paramL
		{
			get { return _params; }
		}
		
		//constructor
		public bashPreview()
		{
			//_wPrompt = null;
			//_bPrompt = null;
			_wizard = null;
		}
		
		//overloaded constructor
//		public bashPreview(string[] input, bool type)
//		{
//			bashEscapes be = new bashEscapes(); //get escape sequences
//			
//			if(type)
//				_bPrompt = input;
//			else
//				_wPrompt = input;
//			for(int x=0;x<input.Length;x++)
//			{
////				if(be.colors.ContainsKey(input[x])
////					setColor(input[x]);
////				else if(be.escapes.ContainsKey(input[x]))
////					setEsc(input[x]);
//			}
//			//create an ordered list in the form:
//				//	<red>	|	/u (user)
//				//	<blue>  |	@/h (hostname)
//				// <green>  |	string 
//		}
		
		public bashPreview(string prmpt)
		{
			_wizard = prmpt;
			
			char[] delims = {'{','}'};
		
			string[] split = _wizard.Split(delims);
			makeColorList(split);
		}
		
		//methods
		private void makeColorList(string[] a)
		{
			//Dictionary<string,string> cList = new Dictionary<string,string>();
			Queue< string> colorQ = new Queue<string>();
			Queue< string> paramQ = new Queue<string>();
			
			bashEscapes be = new bashEscapes();
//			string[] cdelims = new string[be.colors.Count];
//			be.colors.Keys.CopyTo(cdelims,0);
			
			//dont think this works...
			for(int x=0;x<a.Length;x++)
			{
				if(be.colors.ContainsKey(a[x]))
					colorQ.Enqueue(a[x]);
			}
			for(int x=1;x<a.Length;x++)
			{//we have to start with x=1 to align correctly
				if(!be.colors.ContainsKey(a[x]))
					paramQ.Enqueue(a[x]);
			}
			//_colors = colorQ;
			//_params = paramQ;
			_colors = colorQ.ToArray();
			//_params = paramQ.ToArray();
			paramPreview(paramQ.ToArray());
		}

		private void paramPreview(string[] b)
		{	
			char[] sp = {'<','>'};
			for(int x=0;x<b.Length;x++)
			{
				pl.Add("");
				string bs = b[x];
				string[] b2 = bs.Split(sp);
				getPreview(b,b2,x);
			}
			
		}//end method
		protected List<string> pl = new List<string>();
		private void getPreview(string[] a,string[] b, int index)
		{
			//List<string> pl = new List<string>();

			//working, but only for 1 param -- must split them up
			//without forgetting which color they go with
			//Queue<string> pParams = new Queue<string>();
			//get escape sequences
			bashEscapes be = new bashEscapes();
		
			//create DateTime instance for use in previews
			DateTime dT = DateTime.Now;
			//iterate over string[]
			//replace every instance of an escape sequence with an example string 
			for(int x=0;x<b.Length;x++)
			{
				if(be.escapes.ContainsKey(b[x]))
				{
					switch(b[x])
					{
						case "asciibell":
							//do something?
							//pParams.Enqueue("");
							break;
						case "date":
							//get date and replace it in array
							pl[index] = pl[index] + dT.ToString("ddd MMM dd");
							//pParams.Enqueue(dT.ToString("ddd MMM dd"));
							break;
						case "hostnameS":
							//get short hostname
							pl[index] = pl[index] + UnixEnvironment.MachineName;
							//pParams.Enqueue(UnixEnvironment.MachineName);
							break;
						case "hostnameF":
							//get hostname.domain
							pl[index] = pl[index] + UnixEnvironment.MachineName+".localdomain";
							//pParams.Enqueue(UnixEnvironment.MachineName+".localdomain");
							break;
						case "susProcs":
							//return an example number
							pl[index] = pl[index] + "0";
							//pParams.Enqueue("0");
							break;
						case "termDev":
							//return tty0 or whatever
							pl[index] = pl[index] + "tty0";
							//pParams.Enqueue("tty0");
							break;
						case "newline":
							//return "\n"
							pl[index] = pl[index] + "\n";
							//pParams.Enqueue("\n");
							break;
						case "carRet":
							//return carriage return "\r"
							pl[index] = pl[index] + "\r";
							//pParams.Enqueue("\r");
							break;
						case "shellExec":
							//return "bash"
							pl[index] = pl[index] + "bash";
							//pParams.Enqueue("bash");
							break;
						case "time24":
							//return time in 24-hour format
							pl[index] = pl[index] + dT.ToString("HH:mm:ss");
							//pParams.Enqueue(dT.ToString("HH:mm:ss"));
							break;
						case "time12":
							//return time in 12-hour format
							pl[index] = pl[index] + dT.ToString("hh:mm:ss");
							//pParams.Enqueue(dT.ToString("hh:mm:ss"));
							break;
						case "time12ampm":
							//return time in 12-hour format (am/pm)
							pl[index] = pl[index] + dT.ToString("hh:mm:ss tt");
							//pParams.Enqueue(dT.ToString("hh:mm:ss tt"));
							break;
						case "username":
							//return current user name
							pl[index] = pl[index] + UnixEnvironment.UserName;
							//pParams.Enqueue(UnixEnvironment.UserName);
							break;
						case "version":
							//return sample short bash version
							pl[index] = pl[index] + "3.1";
							//pParams.Enqueue("3.1");
							break;
						case "version2":
							//return sample long bash version
							pl[index] = pl[index] + "3.1.17";
							//pParams.Enqueue("3.1.17");
							break;
						case "dir":
							//return sample working directory
							pl[index] = pl[index] + "/sample/wDir";
							//pParams.Enqueue("/sample/wDir");
							break;
						case "dirBase":
							//return short sample working dir
							pl[index] = pl[index] + "wDir";
							//pParams.Enqueue("wDir");
							break;
						case "cmdHist":
							pl[index] = pl[index] + "500";
							//pParams.Enqueue("500");
							//return sample number
							break;
						case "cmdNum":
							pl[index] = pl[index] + "10";
							//pParams.Enqueue("10");
							//return sample number
							break;
						case "su":
							//return $ or #
							if(UnixEnvironment.UserName == "root") { pl[index] = pl[index] + "#"; /*pParams.Enqueue("#");*/ break; }
							else { pl[index] = pl[index] + "$"; /*pParams.Enqueue("$");*/ break; }
					}//end switch
				}//end if
				else
				{
					pl[index] = pl[index] + b[x];
					//pParams.Enqueue(b[x]);
				}//end else
				
			}//end for
			//_params = pParams.ToArray();
			_params = pl.ToArray();
		}//end method
		
	}//end class
}//end namespace
